//
//  YDOALoginVC.swift
//  YDOAProduct
//
//  Created by YDWY on 2017/5/25.
//  Copyright © 2017年 Allen. All rights reserved.
//

import UIKit
import Alamofire


//http://ydxzoa.xgyuanda.com/api.php/Index/login?token=ydxzoa&name=%E6%88%91%E6%98%AF%E4%BF%A1%E6%81%AF%E4%B8%AD%E5%BF%83%E8%81%8C%E5%91%98&pass=123456
//http://ydxzoa.xgyuanda.com/api.php/Index/login?

class YDOALoginVC: UIViewController {
    
    
    lazy var userDefault : UserDefaults = {() in
        return UserDefaults.standard;
    }()
    
    @IBOutlet weak var headerImg: UIImageView!
    @IBOutlet weak var usernameTF: YMTextField!
    @IBOutlet weak var passwordTF: YMTextField!
    @IBOutlet weak var regsterBtn: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
    }
    
    
    
    private func setupUI(){
        usernameTF.delegate = self
        passwordTF.delegate = self
        usernameTF.text = "我是主管"
        passwordTF.text = "123456"
        
        let userNameLeftView = UIImageView(frame: CGRect(x: 0, y: 0, width: 15, height: 21))
        usernameTF.setValue( UIColor.white, forKeyPath: "_placeholderLabel.textColor")
        
        userNameLeftView.image = UIImage(named:"login_user")
        usernameTF.leftView = userNameLeftView
        usernameTF.leftViewMode = .always
        
        let passwordLeftView = UIImageView(frame: CGRect(x: 0, y: 0, width: 15, height: 21))
        
        passwordTF.leftView = passwordLeftView
        passwordLeftView.image = UIImage(named:"login_password")
        passwordTF.leftViewMode = .always

        passwordTF.setValue( UIColor.white, forKeyPath: "_placeholderLabel.textColor")
        regsterBtn.layer.borderWidth = 1
        regsterBtn.layer.borderColor = UIColor(205, 201, 187).cgColor
        
    }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    ///联系客服
    @IBAction func callCnetr(_ sender: Any) {

        
    }
    ///登录
    @IBAction func login(_ sender: Any) {
        if usernameTF.text == "" || passwordTF.text == ""{
            self.presentModelView(msg: "用户名和密码不能为空")
        }else{
         let params = [
                "name":usernameTF.text!,
                "pass":passwordTF.text!,
                ]
            
            YMNetworkManager().postRequset(urlString: LOGINING, params: params, success: { (dataSoure) in
                if !dataSoure.isEmpty && dataSoure["status"]?.int8Value == 1{
                    print(dataSoure);
//                    let userinfo =  dataSoure["data"] as! NSMutableDictionary
//                    userinfo.setValue("token", forKey: "token")
                    self.userDefault.set(dataSoure["data"], forKey: USERINFO)
                    let homeVc = YDOAHomeVC();
                    homeVc.dataSource = dataSoure["data"] as? [String : AnyObject]
                    self.present(homeVc, animated: true, completion: {
                        
                    })
                }else{
                    self.presentModelView(msg: dataSoure["message"] as! String)
                }
           }, failture: { (error) in
            
           })
        }
    }
    ///注册
    @IBAction func register(_ sender: UIButton) {
        
        
    }
    
    
    //    MARK : 弹出提示视图
    func presentModelView(msg:String){
        let  vc = LoginPresentVC()
        vc.modalPresentationStyle = .overCurrentContext //模态视图的展现形式
        vc.msg = msg
        vc.view.backgroundColor = UIColor(white: 0.3, alpha: 0.7) //视图的背景颜色
        self.present(vc, animated: true) {
            
        }
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}


extension YDOALoginVC : UITextFieldDelegate {

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    
}

